
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "NSRefDataTool.h"

#include "MainFrm.h"

#include "NSRefDataToolDoc.h"
#include "NSRefDataToolView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_COMMAND(ID_FILE_OPEN, &CMainFrame::OnFileOpen)
	ON_COMMAND(ID_FILE_CLOSE2, &CMainFrame::OnFileClose)
	ON_COMMAND(ID_PATCH_START, &CMainFrame::OnPatchStart)
END_MESSAGE_MAP()

// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;

	cs.lpszName = L"VSRefDataTool";
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE
		 | WS_MINIMIZEBOX | WS_SYSMENU;

	cs.style &= ~FWS_ADDTOTITLE;

	return TRUE;
}

// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}
#endif //_DEBUG


// CMainFrame message handlers

void CMainFrame::OnFileOpen()
{
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"excel files (*.xlsx, *.xlsm)|*.xlsx;*.xlsm|");

	if (dlg.DoModal() == IDOK)
	{
		CNSRefDataToolDoc* doc = static_cast<CNSRefDataToolDoc*>(GetActiveDocument());
		if (doc->Open(dlg.GetPathName()) == true) {
			std::wstring title(L"VSRefDataTool - ");
			title.append(dlg.GetFileName());
			SetWindowText(title.c_str());
		} else {
			Log log(Log::Error);
			log.append(dlg.GetFileName());
			log.append(L" open failed");
			doc->WriteLog(log);
		}
	}
}

void CMainFrame::OnFileClose()
{
	CNSRefDataToolDoc* doc = static_cast<CNSRefDataToolDoc*>(GetActiveDocument());
	doc->Close();
	SetWindowText(L"VSRefDataTool");
}

void CMainFrame::OnPatchStart()
{
	// TODO: Add your command handler code here
	CNSRefDataToolDoc* doc = static_cast<CNSRefDataToolDoc*>(GetActiveDocument());
	doc->StartPatch();
}
